/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sdd.dispatchers;

import com.sdd.facades.view.CategoryViewFacade;
import com.sdd.plains.CategoryPlain;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author tpgalan
 */
@WebServlet(name = "Dispatcher", urlPatterns = {"/cat"})
public class Dispatcher extends HttpServlet {

    @EJB
    CategoryViewFacade categoryViewFacade;

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String catId = request.getParameter("id");
            out.println("<html><body>");
            if (catId != null && !catId.equalsIgnoreCase("")) {
                Long id = new Long(catId);
                CategoryPlain requested = categoryViewFacade.getCategory(id);
                out.print("The requested category was: " + requested.getCategoryId() + ": " + requested.getName() + "<br>");
                List<CategoryPlain> subcategories = requested.getSubcategories();
                for (CategoryPlain categoryPlain : subcategories) {
                    out.print(categoryPlain + "<br>");
                }
            }
            out.print("The main categories are:");
            List<CategoryPlain> allMainCategories = categoryViewFacade.getAllMainCategories();
            out.print("<ol>");
            for (CategoryPlain categoryPlain : allMainCategories) {
                out.println("<li>" + categoryPlain + "</li>");
            }
            out.print("</ol>");

            out.print("The main categories are (with CB):");
            List<CategoryPlain> allMainCategories2 = categoryViewFacade.getAllMainCategoriesWithCB();
            out.print("<ol>");
            for (CategoryPlain father : allMainCategories2) {
                out.println("<li>" + father + "</li>");
                List<CategoryPlain> childs = categoryViewFacade.getChildCategories(father);
                if (childs != null && !childs.isEmpty()) {
                    out.print("<ol>");
                    for (CategoryPlain child : childs) {
                        out.println("<li>" + child + "</li>");
                    }
                    out.print("</ol>");
                }
            }
            out.print("</ol>");

            out.println("<form action='' method='post'>");
            out.println("<p>Category name<input type='text' name='catname'/></p>");
            out.println("<p>Category (father) ID: <input type='text' name='catfather'/></p>");
            out.println("<p><input type='submit' value='create'/></p>");
            out.println("</form>");
            out.println("</body></html>");
        } finally {
            out.close();
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        CategoryPlain c = new CategoryPlain();
        String fatherId = request.getParameter("catfather");
        String name = request.getParameter("catname");
        CategoryPlain returned = null;
        c.setName(name);
        if (fatherId != null && !fatherId.equalsIgnoreCase("")) {
            CategoryPlain fatherPlain = categoryViewFacade.getCategory(Long.valueOf(fatherId));
            returned = categoryViewFacade.addCategoryWithFather(c, fatherPlain);
        } else {
            returned = categoryViewFacade.addCategory(c);
        }

        response.sendRedirect("/SDDStore-war/cat?id=" + returned.getCategoryId());

    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
